/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using DaveSexton.DocProject.Sandcastle.Resources;

namespace DaveSexton.DocProject.Sandcastle.Configuration
{
	[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface")]
	public sealed class PresentationCollection : ConfigurationElementCollection
	{
		#region Public Properties
		public new Presentation this[string name]
		{
			get
			{
				if (name == null)
					throw new ArgumentNullException("name");

        return GetPresentation(name);
			}
		}

		public Presentation this[int index]
		{
			get
			{
				return this.BaseGet(index) as Presentation;
			}
		}
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="PresentationCollection" /> class.
		/// </summary>
		public PresentationCollection()
		{
		}
		#endregion

		#region Methods
		public bool Contains(string name)
		{
			return !string.IsNullOrEmpty(name) && this.BaseGet(name) as Presentation != null;
		}

		protected override ConfigurationElement CreateNewElement()
		{
			return new Presentation();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			Presentation presentation = element as Presentation;

			if (presentation == null)
				throw new ArgumentException(Errors.ConfigElementNotPresentationElement);

			return presentation.Name;
		}

		public void EnsureExists(string name)
		{
			GetPresentation(name);
		}

		private Presentation GetPresentation(string name)
		{
			Presentation presentation = this.BaseGet(name) as Presentation;

			if (presentation == null)
				throw new ArgumentException(string.Format(System.Globalization.CultureInfo.CurrentCulture,
					Errors.InvalidPresentationName, name), "name");

			return presentation;
		}
		#endregion
	}
}
